package NodeList;

/**
 * @ Author     ：侯耀祖
 * @ Description：
 */
public class getIntersectionNode {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        int lenA = getSize(headA);
        int lenB = getSize(headB);
        int p = Math.abs(lenA-lenB);
        boolean flag = true;
        int f = 1;
        ListNode node;
        ListNode nodeFast;
        ListNode result = new ListNode();
        if (lenA<=lenB){
            node = headA;
            nodeFast = headB;
        } else {
            node = headB;
            nodeFast = headA;
        }
        while (nodeFast != null){
            if (p > 0){
                nodeFast = nodeFast.next;
                p--;
            } else if (nodeFast == node){
                return node;
            } else {
                node = node.next;
                nodeFast = nodeFast.next;
            }
        }
        return null;
    }
    public int getSize(ListNode head){
        ListNode node;
        node = head;
        int count = 0;
        while (node != null){
            count++;
            node = node.next;
        }
        return count;
    }
}
